package com.headwind.logistics.permission.service.impl;

import com.headwind.logistics.common.util.Md5Util;
import com.headwind.logistics.common.util.getV4IPUtil;
import com.headwind.logistics.log.mapper.LogMapper;
import com.headwind.logistics.log.pojo.LoginLog;
import com.headwind.logistics.permission.dto.EmpRoleDTO;
import com.headwind.logistics.permission.mapper.EmployeeMapper;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.pojo.MenuVo;
import com.headwind.logistics.common.pojo.Role;
import com.headwind.logistics.permission.service.intl.EmployeeServiceIntl;
import com.headwind.logistics.permission.util.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 业务逻辑层
 */
@Service
public class EmployeeServiceImpl implements EmployeeServiceIntl {

    //员工数据访问层
    @Resource
    private EmployeeMapper employeeMapper;

    //日志数据访问层
    @Resource
    private LogMapper logMapper;

    /**
     * 后台管理登录
     * @param employee
     * @return
     */
    @Override
    @Transactional  //事务管理
    public Employee adminLogin(Employee employee) {


        //获取当前系统
        Properties props = System.getProperties();//获得系统属性集
        String osName = props.getProperty("os.name");//操作系统名称     

        LoginLog loginLog = new LoginLog();
        //获取当前登录ip
        loginLog.setLoginIp(getV4IPUtil.getV4IP());
        loginLog.setLoginSystem(osName);
        loginLog.setLoginName(employee.getEmployeeNo());
        loginLog.setLoginType(2);

        //添加登录日志
        int count = logMapper.addLoginLog(loginLog);


        return employeeMapper.adminLogin(employee);
    }

    /**
     * 根据员工号查询菜单记录   返回layui后台菜单需要的格式
     * @param employeeNo
     * @return
     */
    @Override
    public Map<String,Object> queryMenusByEmployeeNo(String employeeNo) {
        Map<String,Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);

        List<MenuVo> menuInfo = new ArrayList<>();
        //需要返回的菜单集合
        List<Menu> subMenus = employeeMapper.queryMenusByEmployeeNo(employeeNo);

        for (Menu menu : subMenus) {
            MenuVo menuVO = new MenuVo();
            menuVO.setId(menu.getMenuId());
            menuVO.setPid(menu.getMenuManager());
            menuVO.setHref(menu.getMenuUrl());
            menuVO.setTitle(menu.getMenuName());
            menuVO.setIcon(menu.getMenuIcon());
            menuVO.setTarget("_self");
            menuInfo.add(menuVO);
        }


        home.put("title","首页");
        home.put("href","/static/admin/page/welcome-3.html");//控制器路由,自行定义
        logo.put("title","逆丰 NF");
        logo.put("image","/static/admin/images/logo.png");
        //logo.put("image","/static/admin/images/logo.jpg");//静态资源文件路径,可使用默认的logo.png
        map.put("homeInfo", home);
        map.put("logoInfo", logo);

        MenuVo me = new MenuVo();
        me.setTitle("常规管理");
        me.setChild(TreeUtil.toTree(menuInfo, 0L));
        me.setIcon("fa fa-address-book");
        me.setHref("");
        me.setTarget("_self");
        List<MenuVo> menuVoList = new ArrayList<>();
        menuVoList.add(me);
        map.put("menuInfo",menuVoList);
        return map;
    }
    /**
     * 获取员工与角色
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<EmpRoleDTO> queryAllEmpRolePage(Integer page, Integer rows) {

        if (page != null){
            page = (page-1)*rows;
        }

        return employeeMapper.queryAllEmpRolePage(page,rows);
    }

    /**
     * 查询全部角色
     * @return
     */
    @Override
    public List<Role> queryAllRole() {
        return employeeMapper.queryAllRole();
    }

    /**
     * 添加员工
     * @param employee
     * @param roleId
     * @return
     */
    @Override
    @Transactional
    public int addEmployee(Employee employee, String roleId) {
        //获取最新的员工号
        Long employeeNo = employeeMapper.queryEmployeeNo();
        //员工密码复制加密md5
        employee.setPassword(Md5Util.getMD5("admin"));

        employee.setEmployeeNo((employeeNo+1)+"");
        //分隔字符串
        String[] roleStr = roleId.split("-");

        int empCount = employeeMapper.addEmployee(employee);
        //插入员工成功
        if (empCount >0){
            for (String ids : roleStr) {
                Long id = Long.parseLong(ids);
                //插入角色员工关联表
                employeeMapper.addEmpRole(employee.getEmployeeId(),id);
            }


        }

        return empCount;
    }

    /**
     * 根据员工id修改员工信息
     * @param employee
     * @return
     */
    @Override
    public int updateEmployeeByEmpId(Employee employee, HttpSession session) {
        String md5Password = null;
        if (employee.getPassword()!= null && !employee.getPassword().equals("")){
            //加密MD5
            md5Password = Md5Util.getMD5(employee.getPassword());

            //赋值密码
            employee.setPassword(md5Password);
            //从session中取出员工信息
            Employee emp = (Employee) session.getAttribute("EMPLOYEE");
            //赋值员工id
            employee.setEmployeeId(emp.getEmployeeId());
        }


        int count = employeeMapper.updateEmployeeByEmpId(employee);

        if (count > 0){
            //从session中取出员工信息
            Employee emp = (Employee) session.getAttribute("EMPLOYEE");

            //赋值邮箱
            if (employee.getEmail() != null && !employee.getEmail().equals("")){
                emp.setEmail(employee.getEmail());
            }
            //赋值性别
            if (employee.getEmployeeSex() != null && !employee.getEmployeeSex().equals("")){
                emp.setEmployeeSex(employee.getEmployeeSex());
            }
            //赋值手机
            if (employee.getEmployeeTel() != null && !employee.getEmployeeTel().equals("")){
                emp.setEmployeeTel(employee.getEmployeeTel());
            }
            //赋值头像
            if (employee.getFaceImage() != null && !employee.getFaceImage().equals("")){
                emp.setFaceImage(employee.getFaceImage());
            }
            //赋值密码
            if (employee.getPassword() != null && !employee.getPassword().equals("")){

                //进行赋值
                emp.setPassword(md5Password);

            }

            session.setAttribute("EMPLOYEE",emp);
        }


        return count;
    }

    /**
     * 根据姓名、性别、职位、入司时间、员工号 搜索员工
     * @param empRoleDTO
     * @return
     */
    @Override
    public List<EmpRoleDTO> queryEmployeeByNameOrSexOrRoleOrTimeOrNo(EmpRoleDTO empRoleDTO,Integer page,Integer rows) {

        if (page != null){
            page = (page-1)*rows;
        }
        //职位搜索条件
        String[] split = null;
        if (null != empRoleDTO.getRoleIdList() && !"".equals(empRoleDTO.getRoleIdList())){
            split = empRoleDTO.getRoleIdList().split("-");
        }

        return employeeMapper.queryEmployeeByNameOrSexOrRoleOrTimeOrNo(empRoleDTO,page,rows,split);

    }

    /**
     * 根据员工id获取角色
     * @param employeeId
     * @return
     */
    @Override
    public List<Role> queryRoleByEmpId(Long employeeId) {
        return employeeMapper.queryRoleByEmpId(employeeId);
    }

    /**
     * 删除或添加员工角色关联表
     * @param roleId
     * @param empId
     * @return
     */
    @Override
    public Integer addOrDeleteEmpRole(Long roleId, Long empId,boolean type) {
        int count = 0;
        if (type){
            count = employeeMapper.addEmpRole(empId, roleId);
        }else{
            count = employeeMapper.deleteEmpRole(roleId, empId);
        }

        return count;
    }

    /**
     * 临时登录获取所有员工
     * @return
     */
    @Override
    public List<Employee> tempLogin() {

        return employeeMapper.tempLogin();
    }

    /**
     * 根据用户获取权限字符串
     * @param employeeId
     * @return
     */
    @Override
    public List<Menu> queryMenuPermsByEmpId(Long employeeId) {
        return employeeMapper.queryMenuPermsByEmpId(employeeId);
    }


}
